from rest_framework import viewsets
from erp_system.models import PermissionsModel
from rest_framework.decorators import action
from rest_framework.response import Response
from ERP.apps.erp_system.serializer.permissions_serializer import PermissionsSerializer
from ERP.utils.base_views import MultipleDestroyMixin


class PermissionsView(viewsets.ModelViewSet, MultipleDestroyMixin):
    """
     create:
    权限--新增

    权限新增, status: 201(成功), return: 新增权限信息

    destroy:
    权限--删除

    权限删除, status: 204(成功), return: None

    multiple_delete:
    权限--批量删除

    权限批量删除, status: 204(成功), return: None

    update:
    权限--修改，修改权限本身的信息（属性）

    权限修改, status: 200(成功), return: 修改增权限信息

    partial_update:
    权限--局部修改，修改权限本身的信息（属性）

    权限局部修改, status: 200(成功), return: 修改增权限信息

    list:
    权限--获取所有的权限

    find_permissions_by_menu:
    查询属于指定菜单或者接口的权限列表，需要参数：menu_id,比如：/find_permissions_by_menu？menu

    返回权限列表，status: 200(成功), return:查询的信息
    """
    queryset = PermissionsModel.objects.all()
    serializer_class = PermissionsSerializer

    @action(methods=['get'], detail=False)
    def find_permissions_by_menu(self, request, *args, **kwargs):
        menu_id = request.query_params.get('menu_id')
        # 这里是关联属性的查询
        permissions_list = PermissionsModel.objects.filter(menu__id=menu_id).all()
        # 序列化
        ser = PermissionsSerializer(instance=permissions_list, many=True)
        return Response(ser.data)
